﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CardLogistics.Model.Basic;
using System.Data.OracleClient;
using System.Data;

namespace CardLogistics.DAL.Basic
{
    /// <summary>
    /// 销售机构/网点信息
    /// </summary>
    public class Basic_SalesOutletData
    {
        #region 新增
        /// <summary>
        /// 新增销售机构/网点信息
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public string Add(Basic_SalesOutlet model)
        {
            try
            {
                OracleParameter[] parameters = {
					new OracleParameter("vID", OracleType.VarChar,100),
					new OracleParameter("vSALESINFOID", OracleType.Number),
					new OracleParameter("vSALESOUTLET_CODE", OracleType.VarChar,50),
					new OracleParameter("vSALESOUTLET_NAME", OracleType.VarChar,50),
					new OracleParameter("vOPERATIONFUNCTION", OracleType.VarChar),
					new OracleParameter("vSTARTDATE", OracleType.DateTime),
					new OracleParameter("vENDDATE", OracleType.DateTime),
                    new OracleParameter("vADDRESS", OracleType.VarChar),
                    new OracleParameter("vTEL", OracleType.VarChar,20),
                    new OracleParameter("vZIP", OracleType.VarChar,10),
					new OracleParameter("vSTATUS", OracleType.Number),
                    new OracleParameter("vISDELETE", OracleType.Number),
                    new OracleParameter("vSALESOUTLET_REMAK", OracleType.VarChar,200),
                                               };
                parameters[0].Direction = ParameterDirection.Output;
                parameters[1].Value = model.Salesinfoid;
                parameters[2].Value = model.Salesoutlet_code;
                parameters[3].Value = model.Salesoutlet_name;
                parameters[4].Value = model.Operationfunction;
                parameters[5].Value = model.Startdate;
                parameters[6].Value = model.Enddate;
                parameters[7].Value = model.Address;
                parameters[8].Value = model.Tel;
                parameters[9].Value = model.Zip;
                parameters[10].Value = model.Status;
                parameters[11].Value = 0;
                parameters[12].Value = model.Salesoutlet_remak;

                string StrOutPar = DbHelperSQL.RunProcedure("P_BASIC_SALESOUTLET_ADD", parameters, parameters[0]).ToString();
                return StrOutPar;
            }
            catch (Exception ex)
            {
                return "err," + ex.Message;
            }
        }
        #endregion

        #region 修改
        /// <summary>
        /// 修改信息
        /// </summary>
        /// <param name="model">实体类</param>
        /// <returns>修改成功返回true、否则返回false</returns>
        public string Update(Basic_SalesOutlet model)
        {
            try
            {
                OracleParameter[] parameters = {
                    new OracleParameter("vID", OracleType.Number),
					new OracleParameter("vSALESINFOID", OracleType.Number),
					new OracleParameter("vSALESOUTLET_CODE", OracleType.VarChar,50),
					new OracleParameter("vSALESOUTLET_NAME", OracleType.VarChar,50),
					new OracleParameter("vOPERATIONFUNCTION", OracleType.VarChar),
					new OracleParameter("vSTARTDATE", OracleType.DateTime),
					new OracleParameter("vENDDATE", OracleType.DateTime),
                    new OracleParameter("vADDRESS", OracleType.VarChar),
                    new OracleParameter("vTEL", OracleType.VarChar,20),
                    new OracleParameter("vZIP", OracleType.VarChar,10),
					new OracleParameter("vSTATUS", OracleType.Number),
                    new OracleParameter("vISDELETE", OracleType.Number),
                    new OracleParameter("vSALESOUTLET_REMAK", OracleType.VarChar,200),
                    new OracleParameter("varOut", OracleType.VarChar,100)};
                parameters[0].Value = model.Id;
                parameters[1].Value = model.Salesinfoid;
                parameters[2].Value = model.Salesoutlet_code;
                parameters[3].Value = model.Salesoutlet_name;
                parameters[4].Value = model.Operationfunction;
                parameters[5].Value = model.Startdate;
                parameters[6].Value = model.Enddate;
                parameters[7].Value = model.Address;
                parameters[8].Value = model.Tel;
                parameters[9].Value = model.Zip;
                parameters[10].Value = model.Status;
                parameters[11].Value = 0;
                parameters[12].Value = model.Salesoutlet_remak;
                parameters[13].Direction = ParameterDirection.Output;
                return DbHelperSQL.RunProcedure("P_BASIC_SALESOUTLET_Update", parameters, parameters[13]).ToString();
            }
            catch (Exception ex)
            {
                return "err," + ex.Message;
            }
        }
        #endregion

        #region 删除
        /// <summary>
        /// 删除信息
        /// </summary>
        /// <param name="id">删除信息的ID</param>
        /// <returns>删除成功返回True。否则返回false</returns>
        public string Delete(int id)
        {
            try
            {
                OracleParameter[] parameters = {
                        new OracleParameter("VarOut",  OracleType.VarChar,100),
					    new OracleParameter("vID",  OracleType.Number)
                };
                parameters[0].Direction = ParameterDirection.Output;
                parameters[1].Value = id;
                string StrOutPar = DbHelperSQL.RunProcedure("P_BASIC_SALESOUTLET_Delete", parameters, parameters[0]).ToString();
                return StrOutPar;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion

        #region 查询
        /// <summary>
        /// 根据条件查询相应的表的字段
        /// </summary>
        /// <param name="StrWhere">查询条件</param>
        /// <param name="StrFields">要查询的字段</param>
        /// <returns>填充完数据的DataTable</returns>
        public DataTable GetList(string StrWhere, string StrFields)
        {
            return CommonData.GetListOrderBy(StrWhere, "basic_SalesOutlet", StrFields, "ID", 0);
        }

        /// <summary>
        /// 根据编号查询基本信息
        /// </summary>
        /// <param name="id">编号</param>
        /// <returns>填充完数据的DataTable</returns>
        public DataTable GetInfoById(string id)
        {
            return GetList(" ID = " + id + " ", "*");
        }
        #endregion

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Basic_SalesOutlet GetModel(string Id)
        {
            Basic_SalesOutlet model = new Basic_SalesOutlet();
            DataRow row = GetInfoById(Id).Rows[0];
            if (row != null)
            {
                if (row["ID"] != null && row["ID"].ToString() != "")
                {
                    model.Id = decimal.Parse(row["ID"].ToString());
                }
                if (row["SALESINFOID"] != null && row["SALESINFOID"].ToString() != "")
                {
                    model.Salesinfoid = decimal.Parse(row["SALESINFOID"].ToString());
                }
                if (row["Salesoutlet_code"] != null && row["Salesoutlet_code"].ToString()!="")
                {
                    model.Salesoutlet_code = row["Salesoutlet_code"].ToString();
                }
                if (row["SALESOUTLET_NAME"] != null)
                {
                    model.Salesoutlet_name = row["SALESOUTLET_NAME"].ToString();
                }
                if (row["OPERATIONFUNCTION"] != null)
                {
                    model.Operationfunction = row["OPERATIONFUNCTION"].ToString();
                }
                if (row["STARTDATE"] != null && row["STARTDATE"].ToString() !="")
                {
                    model.Startdate = DateTime.Parse(row["STARTDATE"].ToString());
                }
                if (row["ENDDATE"] != null && row["ENDDATE"].ToString() != "")
                {
                    model.Enddate = DateTime.Parse(row["ENDDATE"].ToString());
                }
                if (row["ADDRESS"] != null)
                {
                    model.Address = row["ADDRESS"].ToString();
                }
                if (row["TEL"] != null)
                {
                    model.Tel = row["TEL"].ToString();
                }
                if (row["ZIP"] != null)
                {
                    model.Zip = row["ZIP"].ToString();
                }
                if (row["STATUS"] != null && row["STATUS"].ToString() != "")
                {
                    model.Status = decimal.Parse(row["STATUS"].ToString());
                }
                if (row["ISDELETE"] != null && row["ISDELETE"].ToString() != "")
                {
                    model.Isdelete = int.Parse(row["ISDELETE"].ToString());
                }
                if (row["Salesoutlet_remak"] != null)
                {
                    model.Salesoutlet_remak = row["Salesoutlet_remak"].ToString();
                }
            }
            return model;
        }
    }
}
